package com.news.gpb.service.impl;

import com.news.gpb.dao.CommentDao;
import com.news.gpb.dao.NewsDao;
import com.news.gpb.dao.UserDao;
import com.news.gpb.entity.Comment;
import com.news.gpb.entity.User;
import com.news.gpb.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("commentService")
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentDao commentDao;
    @Autowired
    UserDao userDao;
    @Autowired
    NewsDao newsDao;
    @Override
    public String addComment(Comment comment) {
        if(commentDao.insertComment(comment)!=null)
        {
            newsDao.updateCommentCount(comment.getNews_id());
            return "success";
        }else {
            return "error";
        }
    }

    @Override
    public List<Comment> getComment(Comment comment) {
        return commentDao.getComment(comment);
    }

    @Override
    public List<User> getPicture(Comment comment) {
        List<Comment> comments=commentDao.getComment(comment);
        List<User> pictures=new ArrayList<>();
        for(Comment com:comments)
        {
            User us = userDao.getUserById(com.getUser_id());
            pictures.add(us);
        }
        return pictures;
    }
}
